//
//  LNMainViewController.m
//  LNDevelopExample
//
//  Created by wwww on 2018/9/7.
//  Copyright © 2018年 淘发现1iOS. All rights reserved.
//
#import "FJFNavigationControllerManager.h"
#import "LNMainViewController.h"
#import "LNNavigationController.h"
#import "LNHomeViewController.h"
#import "LNNewsViewController.h"
#import "LNSearchViewController.h"
#import "LNProfileViewController.h"
#import "LNTheme.h"


@interface LNMainViewController ()
/// viewModel
@property (nonatomic, readonly, strong) LNMainViewModel *viewModel;
@end

@implementation LNMainViewController

@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /// 初始化所有的子控制器
    [self _setupAllChildViewController];
    
    /// set delegate
    self.tabBarController.delegate = self;
}

+(void)initialize{
    // 设置整个应用中的 UITabBarItem 按钮的颜色
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        UITabBarItem *item = [UITabBarItem appearance];
        LNAppConfigManager *app = [LNAppConfigManager getConfig];
        if(app && app.is_Tab){
            NSDictionary *normalAttr = @{NSForegroundColorAttributeName:LNColorFromHexString(app.tabnorColor),
                                         NSFontAttributeName:LNRegularFont_10};
            NSDictionary *selectedAttr = @{NSForegroundColorAttributeName:LNColorFromHexString(app.tabselectColor),
                                           NSFontAttributeName:LNRegularFont_10};
            [item setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
            [item setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
        }
        else{
            NSDictionary *normalAttr = @{NSForegroundColorAttributeName:LNColorFromHexString(@"#303033"),
                                         NSFontAttributeName:LNRegularFont_10};
            NSDictionary *selectedAttr = @{NSForegroundColorAttributeName:LN_MAIN_TINTCOLOR,
                                           NSFontAttributeName:LNRegularFont_10};
            [item setTitleTextAttributes:normalAttr forState:UIControlStateNormal];
            [item setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
        }
    });
}


#pragma mark - 初始化所有的子视图控制器
- (void)_setupAllChildViewController{
    
    ///首页
    UINavigationController *homeNavigationController = ({
        LNHomeViewController *homeViewController = [[LNHomeViewController alloc] initWithViewModel:self.viewModel.homeViewModel];
        
        LNTabBarItemTagType tagType = LNTabBarItemTagTypeHome;
        /// 配置
        [self configViewControoler:homeViewController title:@"首页" norbgImage:@"home_tab_btn_home_off" selectbgImage:@"home_tab_btn_home_on" itemTag:tagType];
        /// 添加到导航栏的栈底控制器
        [FJFNavigationControllerManager navigationControllerWithRootViewController:homeViewController];
    });
    
    /// 新的
    UINavigationController *newsNavigationController = ({
        LNNewsViewController *newsViewController = [[LNNewsViewController alloc] initWithViewModel:self.viewModel.newsViewModel];
        
        LNTabBarItemTagType tagType = LNTabBarItemTagTypeNews;
        /// 配置
        [self configViewControoler:newsViewController title:@"新的" norbgImage:@"home_tab_btn_new_off" selectbgImage:@"home_tab_btn_new_on" itemTag:tagType];
        [FJFNavigationControllerManager navigationControllerWithRootViewController:newsViewController];
    });
    
    /// 搜索
    UINavigationController *searchNavigationController = ({
        LNSearchViewController *searchViewController = [[LNSearchViewController alloc] initWithViewModel:self.viewModel.serachViewModel];
        
        LNTabBarItemTagType tagType = LNTabBarItemTagTypeSearch;
        /// 配置
        [self configViewControoler:searchViewController title:@"搜索" norbgImage:@"home_tab_btn_canyu_off" selectbgImage:@"home_tab_btn_canyu_on" itemTag:tagType];
        [FJFNavigationControllerManager navigationControllerWithRootViewController:searchViewController];
    });
    
    /// 我的
    UINavigationController *profileNavigationController = ({
        LNProfileViewController *profileViewController = [[LNProfileViewController alloc] initWithViewModel:self.viewModel.profileViewModel];
        
        LNTabBarItemTagType tagType = LNTabBarItemTagTypeProfile;
        /// 配置
        [self configViewControoler:profileViewController title:@"我的" norbgImage:@"home_tab_btn_my_off" selectbgImage:@"home_tab_btn_my_on" itemTag:tagType];
        [FJFNavigationControllerManager navigationControllerWithRootViewController:profileViewController];
    });
    
    /// 添加到tabBarController的子视图
    self.tabBarController.viewControllers = @[ homeNavigationController, newsNavigationController, searchNavigationController, profileNavigationController ];
    
    /// 配置栈底
    [LNSharedAppDelegate.navigationControllerStack pushNavigationController:homeNavigationController];
}

#pragma mark - 配置ViewController

-(void)configViewControoler:(UIViewController *)viewController  title:(NSString *)title norbgImage:(NSString *)norbgImage selectbgImage:(NSString *)selectbgImage itemTag:(LNTabBarItemTagType)tagType{
    
    LNThemeTabItem *Item =  [[LNTheme shareThemeManager] getThemeWithTag:tagType];
    [viewController.tabBarItem ln_setImageWithURL:Item.norbgImage placeholderImage:[UIImage imageNamed:norbgImage]];
    [viewController.tabBarItem ln_setSelectImageWithURL:Item.selectbgImage placeholderImage:[UIImage imageNamed:selectbgImage]];
    viewController.tabBarItem.title = Item==nil?title:Item.title;
    [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 0)];
    [viewController.tabBarItem setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
}


//#pragma mark - UITabBarControllerDelegate
//- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    /// 需要判断是否登录
//    if ([[self.viewModel.services client] isLogin]) return YES;
//
//    MHTabBarItemTagType tagType = viewController.tabBarItem.tag;
//
//    switch (tagType) {
//        case MHTabBarItemTagTypeHome:
//        case MHTabBarItemTagTypeConsign:
//            return YES;
//            break;
//        case MHTabBarItemTagTypeMessage:
//        case MHTabBarItemTagTypeProfile:
//        {
//            @weakify(self);
//            [[self.viewModel.services client] checkLogin:^{
//                @strongify(self);
//                self.tabBarController.selectedViewController = viewController;
//                [self tabBarController:tabBarController didSelectViewController:viewController];
//            } cancel:NULL];
//            return NO;
//        }
//            break;
//    }
//    return NO;
//}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"viewController   %@  %zd",viewController,viewController.tabBarItem.tag);
    [LNSharedAppDelegate.navigationControllerStack popNavigationController];
    [LNSharedAppDelegate.navigationControllerStack pushNavigationController:(UINavigationController *)viewController];
}

@end
